//
//  CZAddPlaceViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/2/17.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZAddPlaceViewController.h"
#import "CityChooseView.h"
@interface CZAddPlaceViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UITextField *receiverTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *provinceTF;
@property (weak, nonatomic) IBOutlet UITextField *detailAddressTF;
@property (weak, nonatomic) IBOutlet UIButton *defaultAddressBtn;
@property (weak, nonatomic) IBOutlet UIButton *expandBtn;

@property (nonatomic, copy) NSString *provinceString;
@property (nonatomic, copy) NSString *cityString;
@property (nonatomic, copy) NSString *districtString;


@end

@implementation CZAddPlaceViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"新增收货地址";
    UIButton *rightBarBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    rightBarBtn.frame = CGRectMake(0, 0, 54, 25);
    [rightBarBtn addTarget:self action:@selector(saveAddress) forControlEvents:(UIControlEventTouchUpInside)];
    rightBarBtn.backgroundColor = mRGBColor(214, 221, 220);
    [rightBarBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    rightBarBtn.layer.cornerRadius = 2;
    rightBarBtn.clipsToBounds = YES;
    rightBarBtn.tintColor = ThemeColor;
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
    self.view.backgroundColor = [UIColor whiteColor];
    self.receiverTF.borderStyle = UITextBorderStyleNone;
    self.phoneTF.borderStyle = UITextBorderStyleNone;
    self.provinceTF.borderStyle = UITextBorderStyleNone;
    self.detailAddressTF.borderStyle = UITextBorderStyleNone;
    self.provinceTF.userInteractionEnabled = NO;
    
    if (self.dataDic) {
        self.receiverTF.text = self.dataDic[@"user_name"];
        NSString *province = self.dataDic[@"province"];
        NSString *city = self.dataDic[@"city"];
        NSString *district = self.dataDic[@"district"];
        self.phoneTF.text = self.dataDic[@"mobile"];
        self.provinceTF.text = [[province stringByAppendingString:city] stringByAppendingString:district];
        self.detailAddressTF.text = self.dataDic[@"address"];
        self.provinceString = province;
        self.cityString = city;
        self.districtString = district;
    }
    
    [self.expandBtn addTarget:self action:@selector(cityViewAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)cityViewAction{
    CityChooseView *cityView = [[CityChooseView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width - 40, (Main_Screen_Width - 40) * 1.8)];
    @weakify(self);
    cityView.SelectCityOk = ^(NSString* province, NSString* citystring, NSString* substring) {
        weak_self.provinceString = province;
        weak_self.cityString = citystring;
        weak_self.districtString = substring;
        weak_self.provinceTF.text = [[province stringByAppendingString:citystring] stringByAppendingString:substring];
    };
    [self.view addSubview:cityView];
}
//
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    if (textField == self.provinceTF && self.provinceTF.isFirstResponder) {
//        [self cityViewAction];
//        [self.view endEditing:YES];
//        return NO;
//    }
//    return YES;
//}

- (IBAction)defaultAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}

- (void)saveAddress {
    if (self.receiverTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入收货人姓名"];
        return;
    }
    if (self.phoneTF.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入收货人手机"];
        return;
    }
    if (self.provinceTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请选择地区"];
        return;
    }
    if (self.detailAddressTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入详细地址"];
        return;
    }
    NSNumber *isDefault;
    if (self.defaultAddressBtn.selected) {
        isDefault = @1;
    } else {
        isDefault = @0;
    }
    
    [SVProgressHUD showWithStatus:@"保存中..."];
    NSDictionary *dic;
    NSString *urlString;
    if (self.dataDic) {
        urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/user-address/update",APP_HOST];
        dic = @{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"user_name" : self.receiverTF.text, @"mobile" : self.phoneTF.text, @"country" : @"china", @"province" : self.provinceString, @"city" : self.cityString, @"district" : self.districtString, @"address" : self.detailAddressTF.text, @"is_default" : isDefault, @"address_id" : self.dataDic[@"address_id"]};
    } else {
        urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/user-address/add",APP_HOST];
        dic = @{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"user_name" : self.receiverTF.text, @"mobile" : self.phoneTF.text, @"country" : @"china", @"province" : self.provinceString, @"city" : self.cityString, @"district" : self.districtString, @"address" : self.detailAddressTF.text, @"is_default" : isDefault};
    }
    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"user_name" : self.receiverTF.text, @"mobile" : self.phoneTF.text, @"country" : @"china", @"province" : self.provinceString, @"city" : self.cityString, @"district" : self.districtString, @"address" : self.detailAddressTF.text, @"is_default" : isDefault} success:^(id response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            if (self.dataDic) {
                [SVProgressHUD showSuccessWithStatus:@"地址编辑成功"];
            } else {
                [SVProgressHUD showSuccessWithStatus:@"添加地址成功"];
            }
            if (self.addAddressCallBack) {
                self.addAddressCallBack();
            }
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
